<template>
  <div class="meal-cart">
    <div class="cart-left" @click="showCartPopup = true">
      <!-- 购物车为空时不显示徽标 -->
      <star-icon name="cart-cart" v-if="contentComputed === 0"></star-icon>
      <van-badge :content="contentComputed" v-else>
        <star-icon name="cart-cart"></star-icon>
      </van-badge>
      <div class="total">
        <div><i>￥</i>{{ totalComputed }}</div>
        <span style="color: #999">打包费:￥{{ packAmount }}</span>
      </div>
    </div>
    <div class="cart-right">
      <button class="checked-btn" @click="squareClick">去结算</button>
    </div>
  </div>
  <!-- 购物车商品详情弹窗 -->
  <van-popup v-model:show="showCartPopup" round position="bottom">
    <div>
      <div class="popup-title">
        <h3>
          已加购商品<span style="color: #ff0000; font-size: 16px"> ￥{{ totalComputed }}</span>
        </h3>
        <span @click="clearCart">清空</span>
      </div>
      <div class="dish-list" v-for="item in cartList" :key="item.id">
        <img :src="item.image" />
        <div class="dish-info">
          {{ item.name }}
          {{ item.dishFlavor ? item.dishFlavor : '' }}
          <div class="dish-amount">
            <span style="color: #ff0000"><i style="font-size: 12px">￥</i>{{ item.amount }}</span>
            <div class="dish-number">
              <van-icon name="minus" @click="deleteCart(item)" />
              <span>×{{ item.number }}</span>
              <van-icon name="plus" @click="addCart(item)" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </van-popup>
</template>

<script setup lang="ts">
import { addCartAPI, clearCartAPI, deleteCartAPI } from '@/api/cart'
import { useCartStore } from '@/stores/modules/cart'
import type { BusinessItemResult } from '@/types/business'
import type { CartItemResult } from '@/types/cart'
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
const props = defineProps<{
  cartList: CartItemResult[]
  business: BusinessItemResult
}>()
const emits = defineEmits<{
  (e: 'getCartList', businessId: number): void
}>()
// 配送费
const packAmount = ref(1)
// 购物车数量
const contentComputed = computed(() => {
  return props.cartList.length
})
// 计算购物车列表总金额
const totalComputed = computed(() => {
  return props.cartList.reduce((total, item) => total + item.amount * item.number, packAmount.value)
})
// 购物车商品详情弹窗
const showCartPopup = ref(false)
const cartStore = useCartStore()

// 添加购物车
const addCart = async (item: CartItemResult) => {
  const params = {
    dishId: item.dishId,
    businessId: props.business.id,
    setmealId: item.setmealId,
    flavors: item.dishFlavor,
  }
  try {
    await addCartAPI(params)
    emits('getCartList', props.business.id)
  } catch (error) {
    console.log(error)
  }
}

// 删除购物车单个商品
const deleteCart = async (item: CartItemResult) => {
  const params = {
    dishId: item.dishId,
    businessId: props.business.id,
    setmealId: item.setmealId,
    dishFlavor: item.dishFlavor,
  }
  try {
    await deleteCartAPI(params)
    emits('getCartList', props.business.id)
  } catch (error) {
    console.log(error)
  }
}

// 清空购物车
const clearCart = async () => {
  try {
    await clearCartAPI()
    showCartPopup.value = false
    cartStore.clearCart()
    emits('getCartList', props.business.id)
  } catch (error) {
    console.log(error)
  }
}
const router = useRouter()
// 跳转订单明细
const squareClick = () => {
  router.push('/order/confirm')
}
</script>

<style scoped>
.meal-cart {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 1;
  min-height: 60px;
  padding: 10px;
  background: var(--box-background);
  display: flex;
  justify-content: space-between;
  box-sizing: border-box;
  border-top: 1px solid #fff;
  box-shadow:
    0 -2px 4px rgba(255, 255, 255, 0.3),
    0 -4px 8px rgba(255, 255, 255, 0.2),
    0 -6px 12px rgba(255, 255, 255, 0.1);
  transition: all 0.3s ease;
}
.cart-left {
  display: flex;
  flex: 1;
}
.total {
  display: flex;
  flex-direction: column;
  font-size: 18px;
  color: #ff0000;
  margin-left: 10px;
  flex: 1;
  i,
  span {
    font-size: 12px;
  }
}
.van-popup {
  padding: 15px;
  min-height: 30%;
  max-height: 60%;
  /* margin-bottom: 60px; */
}
.popup-title {
  display: flex;
  justify-content: space-between;
  span {
    font-size: 12px;
    color: #999;
  }
}
.dish-list {
  display: flex;
  margin-top: 15px;
  img {
    height: 60px;
    width: 60px;
  }
  .dish-info {
    flex: 1;
    margin-left: 5px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
  }
  .dish-amount {
    display: flex;
    justify-content: space-between;
  }
  .dish-number {
    width: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #999;
    border-radius: 10px;
    span {
      flex: 1;
      text-align: center;
      border-left: 1px solid #999;
      border-right: 1px solid #999;
    }
  }
}
.checked-btn {
  color: #fff;
  width: 90px;
  height: 30px;
  font-size: 12px;
  border-radius: 20px;
  background: var(--btn-background);
}
</style>
